package com.example.web_back.controller;

import com.example.web_back.entity.Complaint;
import com.example.web_back.mapper.ComplaintMapper;
import com.example.web_back.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * User控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/complaint")
public class ComplaintController {

    @Autowired
    private ComplaintMapper complaintMapper;

    @PostMapping("/submit")
    public Result submitComplaint(@RequestBody Complaint complaint) {
        complaintMapper.insert(complaint);
        return Result.success();
    }

    @PostMapping("/getAllComplaints")
    public List<Complaint> getAllComplaints() {
        List<Complaint> complaintList = complaintMapper.selectList(null);
        System.out.println(complaintList.get(0));
        return complaintList;
    }

    //@Transactional
    @PostMapping("/saveComplaint")
    public Result saveComplaint(@RequestBody Complaint complaint) {
        if (complaint.getComplaintid() != null) {
            complaintMapper.updateById(complaint);
        } else {
            complaintMapper.insert(complaint);
        }
        return Result.success();
    }
}